package com.fc.test.service;

import java.util.List;
import java.util.Arrays;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fc.test.mapper.auto.StuInfoMapper;
import com.fc.test.model.auto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.StuCvMapper;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;

/**
 * 学生简历表 StuCvService
 * @Title: StuCvService.java 
 * @Package com.fc.test.service 
 * @author DrRic_自动生成
 * @email drhyz@qq.com
 * @date 2020-07-05 17:21:51  
 **/
@Service
public class StuCvService implements BaseService<StuCv, StuCvExample>{
	@Autowired
	private StuCvMapper stuCvMapper;
	@Autowired
	private StuInfoMapper stuInfoMapper;
	
      	      	   	      	      	      	      	      	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<Object> list(Tablepar tablepar,StuCv record){
	        StuCvExample testExample=new StuCvExample();
	        testExample.createCriteria().andLikeQuery(record);
	        testExample.setOrderByClause("cvid ASC");
			if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        }
	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<StuCv> list= stuCvMapper.selectByExample(testExample);
		 	JSONArray result = new JSONArray();
			for(StuCv tmp : list){
				JSONObject rec = new JSONObject();
				rec.put("StuCv", tmp);
				StuInfo stuInfo = stuInfoMapper.selectByPrimaryKey(tmp.getSid());
				if(stuInfo!=null){
					stuInfo.setPassword(null);
					stuInfo.setAccount(null);
					stuInfo.setStatus(null);
					rec.put("StuInfo", stuInfo);
				}
				else {
					rec.put("StuInfo", new StuInfo());
				}

				result.add(rec);
			}

		 	return new PageInfo<>(result);
	 }

	public PageInfo<StuCv> listForSys(Tablepar tablepar, StuCv record){
		StuCvExample testExample=new StuCvExample();
		testExample.createCriteria().andLikeQuery(record);
		testExample.setOrderByClause("cvid ASC");
		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		}
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<StuCv> list= stuCvMapper.selectByExample(testExample);
		PageInfo<StuCv> pageInfo = new PageInfo<StuCv>(list);
		return  pageInfo;
	}

	/**
	 * 模糊查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<StuCv> search(String keyword){
//		StuCvExample testExample=new StuCvExample();
//		StuCv record = new StuCv();
		//模糊查询支持以下几种类型
//		record.setExpectSalary(keyword);
//		record.setInterest(keyword);
//		record.setJobObjective(keyword);
//		record.setSkill(keyword);
//		record.setEducation(keyword);
//		testExample.createCriteria().andLikeQuery(record);
//		testExample.setOrderByClause("cvid ASC");
//		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
//			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
//		}
//		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<StuCv> list= stuCvMapper.mhSearch(keyword);
		return  list;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			StuCvExample example=new StuCvExample();
			example.createCriteria().andCvidIn(stringB);
			return stuCvMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public StuCv selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return stuCvMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(StuCv record) {
		return stuCvMapper.updateByPrimaryKeySelective(record);
	}

	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(StuCv record) {
				
		record.setCvid(null);
		
				
		return stuCvMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(StuCv record, StuCvExample example) {
		
		return stuCvMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(StuCv record, StuCvExample example) {
		
		return stuCvMapper.updateByExample(record, example);
	}

	@Override
	public List<StuCv> selectByExample(StuCvExample example) {
		
		return stuCvMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(StuCvExample example) {
		
		return stuCvMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(StuCvExample example) {
		
		return stuCvMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param stuCv
	 * @return
	 */
	public int checkNameUnique(StuCv stuCv){
		StuCvExample example=new StuCvExample();
		example.createCriteria().andSidEqualTo(stuCv.getSid());
		List<StuCv> list=stuCvMapper.selectByExample(example);
		return list.size();
	}


}
